// subpage/pages/goodsList/goodsList.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cid:'',
    query:'',
    pagenum:1,
    pagesize:10,
    total:0,
    goodsList:[],
    isRequest:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id)
    this.setData({
      cid:options.id
    })
    this.getDate()
  },


    getDate(bool){
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        isRequest:true
      })
      wx.request({
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',
        data:{
          cid:this.data.cid,
          query:this.data.query,
          pagenum:this.data.pagenum,
          pagesize:this.data.pagesize
        },
        success:res=>{
          if(bool){
            this.setData({
              total:res.data.message.total,
              goodsList:[...this.data.goodsList,
                ...res.data.message.goods]
              })
          }else{
            this.setData({
            total:res.data.message.total,
            goodsList:res.data.message.goods
            })
          }
         
          wx.hideLoading({
            success: (res) => {},
          })
          this.setData({
            isRequest:false
          })

          // 结束下拉刷新
         wx.stopPullDownRefresh({
           success: (res) => {},
         })
        }
      })
    },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      query:'',
      pagenum:1,
      pagesize:10,
    })
    this.getDate()
  },

  /**
   * 页面上拉触底事件的处理函数,上滑加载更多
   */
  onReachBottom: function () {
    if(this.data.isRequest){
      return
    }
    // 阻止继续滑动加载空的数据
    if(this.data.total>this.data.goodsList.length){
      //修改页码
      this.setData({
      pagenum:this.data.pagenum+1
    })
    this.getDate(true)
    }
  },
  button(e){
    wx.navigateTo({
      url: '/subpage/pages/goodsinfo/goodsinfo?id='+e.detail.id,
    })
  }
})